"""
主页
"""
from django.shortcuts import render, HttpResponse, redirect
from django.http import JsonResponse
from datetime import datetime, date
from sa import models
# 导入首页类
from sa.forms.home_data import IndexData


def index(request):
    return render(request, 'index.html')


def chart_line(request):
    """ 构造折线图 统计之前6个月,3个RO的Project数量合计 """
    # 实例化对象
    project_data = IndexData(login_sa=request.sa)
    if request.sa.role == 'G_M':
        project_counts = project_data.get_project_count()
        legend_data = ['ERO', 'SRO', 'NRO']
        series_data = [
            {
                'name': 'ERO',
                'type': 'line',
                # 不需要堆叠效果，移除stack: 'Total'属性。
                # 'stack': 'Total',
                # 'data': [120, 132, 101, 134, 90, 230]
                # "data": get_RO_count['ERO_projects']   如果没有会报异常
                'data': project_counts.get('ERO_projects', 0),
            },
            {
                'name': 'SRO',
                'type': 'line',
                # 'stack': 'Total',
                # 'data': [150, 232, 201, 154, 190, 330]
                'data': project_counts.get('SRO_projects', 0),
            },
            {
                'name': 'NRO',
                'type': 'line',
                # 'stack': 'Total',
                # 'data': [220, 182, 191, 234, 290, 330]
                'data': project_counts.get('NRO_projects', 0),
            },

        ]
        result = {
            'title_text': '前6月各RO的Project数量统计',
            'status': True,
            'legend': legend_data,
            'series': series_data,
            # X轴数据也可以从类里拿到需要的月份列表，这里是包含当前月和之前5个月
            'xAxis': project_data.months,
        }
        return JsonResponse(result)

    elif request.sa.role == 'user':
        # 类的个人项目统计数量方法
        project_counts = project_data.get_personal_project_count()
        legend_data = [request.sa.SA_CName]
        series_data = [
            {
                # 'name': 'ERO',
                'name': request.sa.SA_CName,
                'type': 'line',
                # 'stack': 'Total',
                # 'data': [120, 132, 101, 134, 90, 230]
                # "data": get_RO_count['ERO_projects']   如果没有会报异常
                'data': project_counts.get('personal_counts', 0),
            },
        ]
        result = {
            'title_text': '前6月个人Project数量统计',
            'status': True,
            'legend': legend_data,
            'series': series_data,
            'xAxis': project_data.months,
        }
        return JsonResponse(result)

    else:
        # 对象执行类的各RO项目统计数量方法
        project_counts = project_data.get_project_count()
        # print('project_counts', project_counts)
        if request.sa.role == 'E_M':
            # ERO_sa_list ERO所有SA名字的列表
            legend_data = project_counts.get('ERO_sa_list', [])
            series_data = []
            for sa_name in legend_data:
                # 获取 ERO_sa_project_counts 字典
                ero_sa_project_counts_dict = project_counts.get('ERO_sa_project_counts', {})
                # 从字典中按名字获取列表
                sa_project_counts = ero_sa_project_counts_dict.get(sa_name, [])
                series_data.append({
                    'name': sa_name,
                    'type': 'line',
                    'data': sa_project_counts,
                })
            result = {
                'title_text': '6个月内ERO各SA Project 数量统计',
                'status': True,
                'legend': legend_data,
                'series': series_data,
                'xAxis': project_data.months,
            }
            return JsonResponse(result)
        if request.sa.role == 'S_M':
            legend_data = project_counts.get('SRO_sa_list', [])
            series_data = []
            for sa_name in legend_data:
                # SRO的字典内容
                sro_sa_project_counts_dict = project_counts.get('SRO_sa_project_counts', {})
                # 再从 sro_sa_project_counts_dict 字典的key(sa_name)拿到列表
                sa_project_counts = sro_sa_project_counts_dict.get(sa_name, [])
                series_data.append({
                    'name': sa_name,
                    'type': 'line',
                    # 堆叠
                    # 'stack': 'Total',
                    'data': sa_project_counts,
                })
            result = {
                'title_text': '前6月SRO Project数量统计',
                'status': True,
                'legend': legend_data,
                'series': series_data,
                'xAxis': project_data.months,
            }
            return JsonResponse(result)
        if request.sa.role == 'N_M':
            legend_data = project_counts.get('NRO_sa_list', [])
            # 初始化 series_data 列表
            series_data = []
            # 循环RO sa列表
            for sa_name in legend_data:
                # 从 data 字典中获取 NRO_sa_project_counts 字典
                nro_sa_project_counts_dict = project_counts.get('NRO_sa_project_counts', {})
                # 从 NRO_sa_project_counts 字典中使用 sa_name 作为键来获取项目数量列表
                sa_project_counts = nro_sa_project_counts_dict.get(sa_name, [])
                series_data.append({
                    'name': sa_name,
                    'type': 'line',
                    # 不需要堆叠效果，移除stack: 'Total'属性。
                    # 这样，每个系列将独立显示，Y轴也会根据当前显示的系列自动调整刻度范围。
                    # 'stack': 'Total',
                    'data': sa_project_counts,
                })

            result = {
                'title_text': '6个月内NRO各SA Project 数量统计',
                'status': True,
                'legend': legend_data,
                'series': series_data,
                'xAxis': project_data.months,
            }
            return JsonResponse(result)


def chart_bar(request):
    """ 构造柱状图数据 """
    # 对象实例化，
    index_data = IndexData(login_sa=request.sa)
    # X轴数据也可以从类里拿到
    # index_data.previous_months()

    # 获取任务用时统计
    if request.sa.role == 'G_M':
        task_total_time = index_data.get_task_total_time()
        legend_data = ['ERO', 'SRO', 'NRO']
        series_data = [
            {
                "name": 'ERO',
                "type": 'bar',
                # "data": get_RO_count['ERO_projects']   #如果没有会报异常
                "data": task_total_time.get('ERO_task_time', 0)
            },
            {
                "name": 'SRO',
                "type": 'bar',
                "data": task_total_time.get('SRO_task_time', 0)
            },
            {
                "name": 'NRO',
                "type": 'bar',
                "data": task_total_time.get('NRO_task_time', 0)
            },

        ]
        # month_data = ['1月', '2月', '3月', '4月', '5月', '6月']
        # month_data = months
        result = {
            'title_text': '前6月各区域Task用时(小时)统计',
            'subtext': str(index_data.current_year) + '年',
            'legend': legend_data,
            'xAxis': index_data.months,
            'series': series_data,
        }
        return JsonResponse({'status': True, 'result': result})
    elif request.sa.role == 'user':
        # 实例对象执行 个人任务时间统计 方法
        task_total_time = index_data.get_personal_task_time()
        legend_data = [request.sa.SA_CName]
        series_data = [
            {
                "name": request.sa.SA_CName,
                "type": 'bar',
                "data": task_total_time.get('personal_task_total_time', 0)
            },
        ]
        result = {
            'title_text': '前6月Task用时(小时)统计',
            'subtext': str(index_data.current_year) + '年',
            'legend': legend_data,
            'xAxis': index_data.months,
            'series': series_data,
        }
        return JsonResponse({'status': True, 'result': result})
    else:
        # 实例对象执行 个人任务时间统计 方法
        task_total_time = index_data.get_task_total_time()
        legend_data = [request.sa.RO]
        if request.sa.role == 'E_M':
            series_data = [
                {
                    "name": request.sa.RO,
                    "type": 'bar',
                    "data": task_total_time.get('ERO_task_time', 0)
                },
            ]
            result = {
                'title_text': '前6月ERO Task用时(小时)总计',
                'subtext': str(index_data.current_year) + '年',
                'legend': legend_data,
                'xAxis': index_data.months,
                'series': series_data,
            }
            return JsonResponse({'status': True, 'result': result})
        if request.sa.role == 'S_M':
            series_data = [
                {
                    "name": request.sa.RO,
                    "type": 'bar',
                    "data": task_total_time.get('SRO_task_time', 0)
                },
            ]
            result = {
                'title_text': '前6月SRO Task用时(小时)总计',
                'subtext': str(index_data.current_year) + '年',
                'legend': legend_data,
                'xAxis': index_data.months,
                'series': series_data,
            }
            return JsonResponse({'status': True, 'result': result})

        if request.sa.role == 'N_M':
            series_data = [
                {
                    "name": request.sa.RO,
                    "type": 'bar',
                    "data": task_total_time.get('NRO_task_time', 0)
                },
            ]
            result = {
                'title_text': '前6月NRO Task用时(小时)总计',
                'subtext': str(index_data.current_year) + '年',
                'legend': legend_data,
                'xAxis': index_data.months,
                'series': series_data,
            }
            return JsonResponse({'status': True, 'result': result})


def chart_pie(request):
    """ 构造饼状图数据 """
    # 实例化并执行方法
    pie = IndexData(login_sa=request.sa)
    tasks = pie.get_task_count()

    db_data = [
        {'value': tasks.get('ERO_Task_count', 0), 'name': 'ERO'},
        {'value': tasks.get('SRO_Task_count', 0), 'name': 'SRO'},
        {'value': tasks.get('NRO_Task_count', 0), 'name': 'NRO'},
    ]

    result = {
        'subtext': str(pie.current_year) + '年',
        'series_data': db_data
    }

    return JsonResponse({'status': True, 'result': result})
